﻿#include "SettingOfChargeTime.h"
#include "ui_settingofchargetime.h"

SettingOfChargeTime::SettingOfChargeTime(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SettingOfChargeTime)
{
    ui->setupUi(this);

    time = new QLabel(ui->frameBottom);
    time->setGeometry(10, 10, 200, 40);
    time->setStyleSheet("QLabel{font: 20px; color: rgb(25, 25, 112);}");
    signal = new QLabel(ui->frameBottom);
    signal->setGeometry(700, 18, 41, 22);
    signal->setStyleSheet("QLabel{background-color:transparent;}");

    QDir dir(IMAGE_PATH);
    if(dir.exists())
    {
        ui->frameTop->setStyleSheet(LOGO_STY);
    }

    for(int i=0; i<GUN_AMOUNT; i++)
    {
        countdown[i] = new CountDownTime(ui->frameMid);
        countdown[i]->setGeometry(710, 10, 30, 40);
        countdown[i]->mysetText("60");
        connect(this->countdown[i], SIGNAL(CoundDownTimeisOver()), this, SLOT(countdownOver()));
        connect(&Refreash_Thread[i], SIGNAL(Device_is_Error(int)), this, SLOT(Device_Error_or_NotConnect(int)));
        connect(&Refreash_Thread[i], SIGNAL(Device_is_NotConnect(int)), this, SLOT(Device_Error_or_NotConnect(int)));
    }

    QRegExp regExpChargingHour("^((\\d)|(1\\d)|(2[0-3]))$");
    QValidator* validatorChargingHour = new QRegExpValidator(regExpChargingHour, 0);
    ui->ChargingHour->setValidator(validatorChargingHour);

    QRegExp regExpChargingMinute("^((\\d)|([1-9]\\d))$");
    QValidator* validatorChargingMiute = new QRegExpValidator(regExpChargingMinute, 0);
    ui->ChargingMinute->setValidator(validatorChargingMiute);

    connect(stackLayout, SIGNAL(currentChanged(int)), this, SLOT(setCountHide(int)));

    keyBoardConnect();
}

SettingOfChargeTime::~SettingOfChargeTime()
{
    delete ui;
}
void SettingOfChargeTime::Device_Error_or_NotConnect(int GUN_NO)
{
    countdown[GUN_NO]->CountDownStop();
}
void SettingOfChargeTime::setCountHide(int current)
{
    if(current == settingOfChargeTimePage)
    {
        ui->_focus_gun_->setText(QString::number(focus_gun_));
        ui->ChargingHour->clear();
        ui->ChargingMinute->clear();
        if(!countdown[focus_gun_-1]->OneSecound->isActive())
            countdown[focus_gun_-1]->CountDownStart();
        for(int i=0; i<GUN_AMOUNT; i++)
        {
            if(focus_gun_-1 != i)
            {
                countdown[i]->hide();
            }
            else
            {
                countdown[i]->show();
            }
        }
    }
}
void SettingOfChargeTime::countdownOver()
{
    /*待完成*/
    CountDownTime* sender_ = static_cast<CountDownTime*>(sender());
    for(int i=0; i<GUN_AMOUNT; i++)
    {
        if(sender_ == countdown[i])
        {
            Refreash_Thread[i].swipCardflag = 0;
        }
    }
}

void SettingOfChargeTime::on_buttonOk_clicked()
{
    /*待完成*/
    int time = ui->ChargingHour->text().toInt()* 60 + ui->ChargingMinute->text().toInt();
     if( ( ui->ChargingHour->text().toInt() > 23 ) ||  ( ui->ChargingMinute->text().toInt() > 59 )  )
     {
     }
     else if(time > 0)
     {
         onStartingCharging(focus_gun_ - 1, 2, 1, 1, time*100,
                            focus_gun_ , GUN_STATE[focus_gun_ - 1].VINflag);
         emit StartCharging_RefreshChargeInfo(focus_gun_);
         GUN_STATE[focus_gun_-1].Page = chargeWatchingDCPage;
         countdown[focus_gun_-1]->CountDownStop();
     }

}

void SettingOfChargeTime::on_buttonBack_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = chargeTypePage;
    countdown[focus_gun_-1]->CountDownStop();
}

void SettingOfChargeTime::on_buttonCancelCharging_clicked()
{
    /*待完成*/
    GUN_STATE[focus_gun_-1].Page = returnChargeGunPage;
    countdown[focus_gun_-1]->CountDownStop();
}

void SettingOfChargeTime::on_Keyboard_clicked()
{
    QPushButton* btn =  static_cast<QPushButton*>(sender());

    QLineEdit* focus = (QLineEdit* )QApplication::focusWidget();

    keyHandle(btn, focus);

}

void SettingOfChargeTime::keyHandle(QPushButton* btn, QLineEdit* lineEdit)
{
    if( btn->text() == tr("空格") )
    {
        lineEdit->insert(" ");
    }
    else if( btn->text() == tr("退格") )
    {
        lineEdit->backspace();
    }
    else if( btn->text() == tr("清除"))
    {
        lineEdit->clear();
    }
    else
    {
        lineEdit->insert(btn->text());
    }
}

void SettingOfChargeTime::keyBoardConnect()
{
    connect(ui->One, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Tow, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Three, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Four, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Five, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Six, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Seven, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Eight, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Nine, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Zero, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->Remove, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
    connect(ui->RemoveAll, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char1, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Char2, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
//    connect(ui->Space, SIGNAL(clicked()), this, SLOT(on_Keyboard_clicked()));
}

